// subpkg_worker/login/login.js
import Toast from '@vant/weapp/toast/toast';
import md5 from '../../utils/md5.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    show:false,
    show1:false,
    careHomeName:'',
    careHomeColumns:['至善养老院','阳光养老院','快乐养老院'],
    identityColumns:['家属','老人','护理员']

  },
  bindAccount(e){
    this.setData({
      account:e.detail.value
    })
  },
  bindPassword(e){
    this.setData({
      password:e.detail.value
    })
  },
  bindPassword1(e){
    this.setData({
      password1:e.detail.value
    })
  },
  login(){
    var identity = ''
    if(this.data.identity==='护理员'){
      identity = 1
    }else if(this.data.identity==='家属'){
      identity = -1
    }else{
      identity = 0
    }
    if(this.data.password!=this.data.password1){
      Toast('两次密码输入不一致!')
      return
    }
    const originalPassword = this.data.password
    const encryptedPassword = md5(originalPassword).toLowerCase()
    wx.request({
      url: 'https://zhixiao.site:8080/client/register',
      data:{
        account:this.data.account,
        password:encryptedPassword,
        // password:this.data.password,
        careHomeName:this.data.careHomeName,
        identity:identity
      },
      success:(res)=>{
        console.log(res)
        var role = res.data.data.role
        if(role === -1){
          wx.setStorageSync('identity', '家属')
        }else if(role === 0){
          wx.setStorageSync('identity', '老人')
        }else{
          wx.setStorageSync('identity', '护工')
        }
        Toast(`${res.data.msg}`)
      }
    })
   
   
  },
  bindHeadhouse(){
    this.setData({
      show:true
    })
  },
  bindIdentity(){
    this.setData({
      show1:true
    })
  },
  onChange(event) {
    var index = event.detail.index
    this.setData({
      careHomeName:this.data.careHomeColumns[index]
    })
  },
  onChange1(event) {
    var index = event.detail.index
    this.setData({
      identity:this.data.identityColumns[index]
    })
  },
  confirm(e){
    console.log(e.detail)
    if(e.detail.index===0){
      this.setData({
        careHomeName:this.data.careHomeColumns[0],
        show:false
      })
      return
    }
    this.setData({
      careHomeName:this.data.careHomeName,
      show:false
    })
  },
  confirm1(e){
    if(e.detail.index===0){
      this.setData({
        identity:this.data.identityColumns[0],
        show1:false
      })
      return
    }
    this.setData({
      identity:this.data.identity,
      show1:false
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  onClose1(){
    this.setData({
      show1:false
    })
  },
  cancel(){
    this.setData({
      show:false
    })
  },
  cancel1(){
    this.setData({
      show1:false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})